package com.cya.lotterysystem.service.activityStatus.operator;

import com.cya.lotterysystem.dao.dataobject.ActivityDO;
import com.cya.lotterysystem.dao.mapper.ActivityMapper;
import com.cya.lotterysystem.dao.mapper.ActivityPrizeMapper;
import com.cya.lotterysystem.service.dto.ConverActivityStatusDTO;
import com.cya.lotterysystem.service.enums.ActivityPrizeStatusEnum;
import com.cya.lotterysystem.service.enums.ActivityStatusEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @author ：陈奕安（3048279304@qq.com）
 * @date ：Created in 2025/11/7 11:58
 * @description：
 * @modified By：
 * @version:
 */
@Component
public class ActivityOperator extends AbstractActivityOperator{

    @Autowired
    private ActivityMapper activityMapper;

    @Autowired
    private ActivityPrizeMapper activityPrizeMapper;

    @Override
    public Integer sequence() {
        return 2;
    }

    @Override
    public Boolean needConvert(ConverActivityStatusDTO converActivityStatusDTO) {

        Long activityId=converActivityStatusDTO.getActivityId();
        ActivityStatusEnum targetStatus=converActivityStatusDTO.getTargetActivityStatus();
        //判断奖品是否抽完
        if (converActivityStatusDTO.getActivityId()==null||null==converActivityStatusDTO.getTargetActivityStatus()){
            return false;
        }
        ActivityDO activityDO = activityMapper.selectById(activityId);
        if (null==activityDO){
            return false;
        }
//        当前活动状态与目标状态是否一致
        if (targetStatus.name().equalsIgnoreCase(activityDO.getStatus())){
            return false;
        }
//        判断奖品是否全部抽完
//        查询init状态的奖品个数
        int count = activityMapper.countPrize(activityId, ActivityPrizeStatusEnum.INIT.name());
        if (count>0){
            return false;
        }


        return true;
    }

    /**
     * 转换状态
     * @param converActivityStatusDTO
     * @return
     */
    @Override
    public Boolean convert(ConverActivityStatusDTO converActivityStatusDTO) {

        try {
            activityMapper.updateStatus(converActivityStatusDTO.getActivityId(),converActivityStatusDTO.getTargetActivityStatus().name());
            return true;
        }catch (Exception e){
            return false;
        }
    }
}